<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Member_model extends CI_Model {
    function __construct()
    {
        parent::__construct();
        $this->gallery_path_image = realpath(APPPATH . '../images');
    }

    public function query_get_member()
    {
        return 
            "SELECT 
                member.*,
                type.member_type,
                type.code,
                type.short_code,
                type.description
            FROM
                psacar_member as member
                LEFT JOIN psacar_member_type as type on member.type_id = type.id";
    }
    
    public function get_member_by_id($dealer_id)
    {        
        $query_string = 
            "SELECT 
                dealer.id,
                dealer.name as dealer_name,
                dealer.contact_phone_1,
                dealer.contact_phone_2,
                dealer.email,
                dealer.website,
                dealer.company,
                dealer.company_phone_1,
                dealer.company_phone_2,
                dealer.address,
                occupation.occupation,
                dealer.description,
                dealer.photo,
                dealer.service,
                location.province,
                location.district,
                type.member_type,
                type.code,
                type.short_code,
                type.description                
            FROM    
                psacar_member as dealer 
                left join psacar_location as location on dealer.location_id = location.id
                left join psacar_occupation as occupation on dealer.occupation_id = occupation.id
                left join psacar_member_type as type on dealer.type_id = type.id
            WHERE        
                dealer.id = '$dealer_id'" . 
            " GROUP BY dealer.id " . 
            " ORDER BY dealer.name";
        
        $result = $this->db->query($query_string);
        return  $result;        
    }
    
    public function get_member_by_email($email) {
        $query_string =  $this->query_get_member() . " WHERE email = '$email'";
        
     	$result = $this->db->query($query_string);
        return $result;        
    }
    
    public function get_member_by_login($login) {
        $query_string =  $this->query_get_member() . " WHERE name = '$login'";
        
     	$result = $this->db->query($query_string);
        return $result;
    }    
    
    public function login($email, $password){        
        $query_string =  $this->query_get_member() . 
            " WHERE
                -- is_active = 1
                -- AND 
                email = '$email' 
                AND password = '$password'";
        
     	$query = $this->db->query($query_string);
        
        if($query->num_rows == 1)
        {
            $row = $query->row();            
            $data = array(
                'userid' => $row->id,
                'name' => $row->name,
                'validated' => true,
                'membership' => $row->type_id,
                'activated' => $row->is_active,
                'type' => $row->code);
            $this->session->set_userdata($data);
            return true;
        }
        
        return false;
    } 
    
    function upload_image(){	  
        $config['upload_path'] = './images/';
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['max_size']	= '10000';
        $config['max_width']  = '1024';
        $config['max_height']  = '1024';
        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        $this->upload->do_upload('image');

        /*
        if ( ! $this->upload->do_upload('image'))
        {
            //echo "not upload";
        }   
        else
        {
            //echo "it upload";
        }
        */        
    }    
    
    public function get_member_list(
        $make = '', 
        $model = '', 
        $name = '',
        $service = '',
        $location = '',
        $year = '')
    {
        $extra = " ";
        $operator = " AND ";
        $this->load->model('Track_model');
        
        if(strlen($make) > 0)
        {            
            $extra .= $operator . "make.id = " . $this->db->escape($make);	

            $this->db->limit(1);
            $query = $this->db->get_where("psacar_make", array("id"=> $make));
            $make = $query->row();            
            $this->Track_model->track_activity("Make", $make->name);
        }
        
        if(strlen($model) > 0)
        {
            $extra .= $operator . "model.id = " . $this->db->escape($model);

            $this->db->limit(1);
            $query = $this->db->get_where("psacar_model", array("id"=> $model));
            $model = $query->row();                        
            $this->Track_model->track_activity("Model", $model->name);
        }
        
        if(strlen($name) > 0)
        {
            $extra .= $operator . "dealer.name = " . $this->db->escape($name);
            $this->Track_model->track_activity("Vendor", $name);
        }

        if(strlen($service) > 0)
        {
            $extra .= $operator . "service.name = " . $this->db->escape($service);
            $this->Track_model->track_activity("Service", $service);
        }

        if(strlen($location) > 0)
        {
            $extra .= $operator . "location.province = " . $this->db->escape($location);
            $this->Track_model->track_activity("Location", $location);
        }

        if(strlen($year) > 0)
        {
            $extra .= $operator . "car.year = " . $this->db->escape($year);
            
            $this->Track_model->track_activity("Year", $year);
        }
        
        $query_string = 
            "SELECT 
                dealer.id as member_id,
                dealer.name as member_name,
                dealer.contact_phone_1,
                dealer.contact_phone_2,
                dealer.email,
                dealer.website,
                dealer.company,
                dealer.company_phone_1,
                dealer.company_phone_2,
                dealer.address,
                occupation.occupation,
                dealer.description,
                dealer.photo,
                dealer.service,
                location.province,
                location.district
            FROM    
                psacar_member as dealer 
                left join psacar_car as car on dealer.id = car.dealer_id
                -- left join psacar_series as series on car.series_id = series.id 
                left join psacar_model as model on car.model_id = model.id 
                left join psacar_make as make on model.make_id = make.id 
                left join psacar_location as location on dealer.location_id = location.id
                left join psacar_occupation as occupation on dealer.occupation_id = occupation.id
                left join psacar_member_service as member_service on dealer.id = member_service.member_id
                left join psacar_service as service on member_service.service_id = service.id
            WHERE        
                (1 = 1)" . $extra .                 
            " GROUP BY dealer.id " . 
            " ORDER BY dealer.name";
                
        $result = $this->db->query($query_string);
        return  $result;        
    }    
}
